<?php

namespace Act\Logic;

use \core\filter\filter;
use \common\Table\ActTable;
use \common\Factory\Factory;

class ActLogic {
    
    private $_observers;
    
    function __construct() {
        $this->_observers = new \SplObjectStorage();
    }
    
    private function insertEvent($appid, $idfa, $eventTimestamp, $db) {
        $result = true;
        $this->_observers->rewind();
        while($this->_observers->valid()) {
            $observersResult = $this->_observers->current()->actChange($appid, $idfa, $eventTimestamp);
            if($observersResult === false) {
                $result = false;
                $db->rollBack();
                break;
            }
            $this->_observers->next();
        }
        $result === true && $db->commit();

        return $result;
    }
    
    public function addObserver($observer) {
        $this->_observers->contains($observer) === false && $this->_observers->attach($observer);
    }
    
    public function insert($data) {
        Factory::getServer('MqWriteLog')->send('act insert raw input params:' . json_encode($data));
        $data['EVENTTIMESTAMP'] = $data['EVENTTIME'];
        //13位时间戳转换为10位时间戳
        strlen($data['EVENTTIME']) == 13 && $data['EVENTTIME'] = substr($data['EVENTTIME'], 0, 10);
        $data['time'] = date('Ym', $data['EVENTTIME']);
        $data['date'] = date('Ymd', $data['EVENTTIME']);
        unset($data['EVENTTIME']);
        //验证及过滤
        $actColumns = array(
            'IDFA' => FILTER_SANITIZE_STRING,
            'APPKEY' => FILTER_SANITIZE_STRING,
            'date' => FILTER_SANITIZE_STRING,
            'time' => FILTER_SANITIZE_STRING,
            'EVENTTIMESTAMP' => FILTER_SANITIZE_STRING,
            'OSVERSION' => FILTER_SANITIZE_STRING,
            'DEVICETYPE' => FILTER_SANITIZE_STRING,
            'TDID' => FILTER_SANITIZE_STRING,
            'IP' => array('filter' => FILTER_VALIDATE_IP),
            'SPREADURL' => FILTER_SANITIZE_STRING,
            'SPREADNAME' => FILTER_SANITIZE_STRING,
            'UA' => FILTER_SANITIZE_STRING,
            'CLICKIP' => array('filter' => FILTER_VALIDATE_IP),
            'CLICKTIME' => FILTER_SANITIZE_STRING,
            'APPSTOREID' => FILTER_SANITIZE_STRING,
            'ADNETNAME' => FILTER_SANITIZE_STRING,
            'channel' => FILTER_VALIDATE_INT,
        );
        $filter = new filter();
        $actColumns = $filter->varArray($data, $actColumns);
        
        if($actColumns === false) {
            Factory::getServer('MqWriteLog')->send('act insert raw input params validate failed:'. json_encode($filter->getErrorItems()));
            return array('success' => false, 'message' => '参数验证不通过');
        }
        
        //入库
        $actColumns = $filter->varArray($data, $actColumns);
        
        $actTable = new ActTable();
        $db = $actTable->getDb();
        $db->beginTransaction();
        $result = $actTable->insert($actColumns);
        if($result) {
            $eventResult = $this->insertEvent($actColumns['APPSTOREID'], $actColumns['IDFA'], $actColumns['EVENTTIMESTAMP'], $db);
            if($eventResult === true) {
                $result = array('success' => true, 'message' => 'ok');
                Factory::getServer('MqWriteLog')->send('act insert database act success');
            }
            else {
                $result = array('success' => false, 'message' => '插入事件执行错误');
            }
        }
        else {
            $result = array('success' => false, 'message' => '数据库插入失败');
            Factory::getServer('MqWriteLog')->send('act insert database act failed');
        }

        return $result;
    }
}